<?php

namespace app\models;

use yii;
use yii\db\Query;
/**
 *后台用户表
 *
 */
class AdminUser extends BaseModel
{
    use \app\lib\Redis;

    private $redis;

    public function __construct()
    {
        $this->table = yii::$app->db->tablePrefix . 'adminuser';
        $this->redis = $this->getInstance();
    }

    public function all($where = [], $offset = false, $limit = false, $order = false)
    {
        // TODO: Implement all() method.
    }

    public function one($where = [])
    {
        // TODO: Implement one() method.
        $db = new Query();
        $db->select('*')->from($this->table);
        if (!empty($where['name'])) $db->andWhere(['name' => $where['name']]);

        return $db->one();
    }

    /**
     * 登录验证
     * @param $name 用户名
     * @param $password 密码
     */
    public function login($name, $password)
    {
        $key = 'all_users';
        if ($user = $this->redis->hget($key, $name)) {
            return json_decode($user, true);
        } else {
            $db = new Query();
            $info = $db->select('*')->from($this->table)->where(['name' => $name, 'password' => $password])->one();
            if ($info) {
                $this->redis->hset($key, $info['id'], json_encode($info));
                return $info;
            }
        }
        return false;
    }

    /**
     * @param int 用户id
     * @return array
     */
    public function get_user_by_id($id)
    {
        $db = new Query();
        return $db->select('a.*,b.role_rules,c.name as zh_name,c.en_name,c.email,c.job_number')
            ->from(yii::$app->db->tablePrefix . "adminuser as a")
            ->join('LEFT JOIN', yii::$app->db->tablePrefix . "adminrole as b", 'a.role=b.id')
            ->join('LEFT JOIN', yii::$app->db->tablePrefix . "staff as c", 'a.staff_id=c.id')
            ->where(['a.id' => $id])
            ->one();
    }

    /**
     * 获取所有用户
     */
    public function get_all_user()
    {
        $db = new Query();
        return $db->select('a.*,b.role_name,c.name as admin_name')
            ->from(yii::$app->db->tablePrefix . "adminuser as a")
            ->join('LEFT JOIN', yii::$app->db->tablePrefix . "adminrole as b", 'a.role=b.id')
            ->join('LEFT JOIN', yii::$app->db->tablePrefix . "staff as c", 'a.staff_id=c.id')
            ->orderBy(['a.time' => SORT_DESC])
            ->all();
    }

    public function get_admin_role($field = '*')
    {
        $db = new Query();
        return $db->select($field)
            ->from(yii::$app->db->tablePrefix . "adminrole")
            ->all();
    }

    /**
     * 添加后台用户
     * @param $data array
     */
    public function add_admin_user($data)
    {
        $db = new Query();
        return yii::$app->db->createCommand()->insert($this->table, $data)->execute();
    }

    /**
     * 通过员工id查找后台用户
     * @param $id int
     */
    public function get_adminuser_by_staff_id($id)
    {
        $db = new Query();
        return $db->select('*')
            ->from($this->table)
            ->where(['staff_id' => $id])
            ->one();
    }

    public function get_adminuser_by_name($name)
    {
        $db = new Query();
        return $db->select('id')
            ->from($this->table)
            ->where(['name' => $name])
            ->one();
    }


    /**
     * 获取user信息
     * @param int $id
     * @return array
     */
    public function getUser($id)
    {
        if (!$info = json_decode($this->redis->hget('all_users', $id), true)) {
            $db = new Query();
            $info = $db->select('*')->from($this->table)->where(['id' => $id])->one();
        }
        return $info;
    }

    /**
     * 更新用户密码
     * @param int $id
     * @param string $password
     */
    public function updateUser($id, $password)
    {
        $redis_key = 'all_users';
        $re = yii::$app->db->createCommand()
            ->update($this->table, ['password' => md5($password)], ['id' => $id])
            ->execute();
        //更新缓存
        if ($re) {
            $userInfo = json_decode($this->redis->hget($redis_key, $id), true);
            $userInfo['password'] = md5($password);
            $this->redis->hset($redis_key, $id, json_encode($userInfo));
            return true;
        }
        return false;
    }

}
